clear all
set more off
cd /Users/zimaoxiao/Dropbox/CC_Yield_Predict/replication_package/  // Navigate to replication folder on your own machine

cap mkdir output

qui {		
* prepare the data
use county_fips year if inrange(year,1950,2020) using data/weather/weather_processed_current, clear
merge 1:1 county_fips year using data/yield/yields_1950_2022, keep(3) keepusing(state_fips corn_yield corn_areaHarv) nogenerate
merge m:1 county_fips using data/geo/us_county_coordinates, keep(3) keepusing(longitude state_name) nogenerate
drop if longitude<-100 | longitude==.
gen lncornyield=log(corn_yield)
drop if lncornyield<=0

sort county_fips state_fips year
bys state_fips year: egen styield=sum(corn_yield)
bys state_fips year: keep if _n==1
keep year state_fips state_name styield
drop if styield==0

* annual growth rate by state
bys state_fips: gen first=1 if _n==1
bys state_fips: gen last=1 if _n==_N
bys state_fips: gen time=_N-1
keep if first==1|last==1
bys state_fips: gen annualgrowthrate=((styield[_n]/styield[_n-1])^(1/time))-1
drop if annualgrowthrate==.
keep state_fips state_name annualgrowthrate

* bar chart of all state's annual growth rate
gsort + annualgrowthrate
egen id = seq()
replace annualgrowthrate=annualgrowthrate*100 // unit: percentage

graph twoway /// 
bar annualgrowthrate id, vertical lc(black) lw(*1.2) fc(*0.7) fi(inten70) || ///
sc annualgrowthrate id, mlab(state_name) mlabpos(12) m(i) mlabc("black") mlabs(*0.5) || ///
, legend(off) xtitle("") xlabel("") ytitle("Yield Annual Growth Rate (%)")
graph export output/FigureS2.jpg, as(jpg) name("Graph") quality(100) replace

}

*** EOF
